<template>
  <div class="all">
    <div class="main">
      <!-- 头部 -->
      <el-row :gutter="20">
        <el-col :span="4"
          ><el-input v-model="input" placeholder="请输入内容"></el-input
        ></el-col>
        <el-col :span="2"><el-button @click="empty">清空</el-button></el-col>
        <el-col :span="15"><el-button type="primary" @click="select">搜索</el-button></el-col>
        <el-col :span="2"
          ><el-button type="success" class="el-icon-edit" @click="dialogFormVisible = true"
            >新增用户</el-button
          ></el-col
        >
      </el-row>
      <!-- 分隔线 -->
      <div class="separate">
        <p><span class="el-icon-info"></span> 共{{ total }}条记录</p>
      </div>
      <!-- 表格 -->
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="id" label="序号" width="180"> </el-table-column>
        <el-table-column prop="email" label="邮箱" width="180">
        </el-table-column>
        <el-table-column prop="phone" label="联系电话"> </el-table-column>
        <el-table-column prop="username" label="用户名" width="180">
        </el-table-column>
        <el-table-column prop="permission_group_id" label="权限组名称" width="180">
        </el-table-column>
        <el-table-column prop="role" label="角色" width="180">
        </el-table-column>
        <el-table-column prop="operation" label="操作" width="180">
          <template slot-scope="scope">
  <el-button type="primary" icon="el-icon-edit" circle  @click="changeInfo(scope.row.id)"></el-button
          ><el-button type="danger" icon="el-icon-delete" circle  @click="open(scope.row.id)"></el-button>
          </template>

        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="block">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage4"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
   <!-- 增加弹出框 -->
   <el-dialog title="添加用户" :visible.sync="dialogFormVisible" >
       <el-form ref="form" :model="form" :rules="rules">
          <el-form-item prop="username" label="用户名" label-width="100px">
            <el-input v-model="form.username" placeholder="请输入用户名" />
          </el-form-item>
          <el-form-item prop="email" label="邮箱" label-width="100px">
            <el-input v-model="form.email" placeholder="请输入邮箱" />
          </el-form-item>
          <el-form-item prop="password" label="密码" label-width="100px">
            <el-input
              v-model="form.password"
              placeholder="请输入密码"
              type="password"
            />
          </el-form-item>
          <el-form-item prop="role" label="角色" label-width="100px">
            <el-input v-model="form.role" placeholder="请输入角色" />
          </el-form-item>
           <el-form-item label="权限组名称">
    <el-select v-model.number="form.permission_group_id" placeholder="活动区域">
      <el-option label="1" value="1"></el-option>
      <el-option label="asds" value="asds"></el-option>
      <el-option label="123" value="123"></el-option>
    </el-select>
  </el-form-item>
          <el-form-item prop="phone" label="联系电话" label-width="100px">
            <el-input v-model="form.phone" placeholder="请输入电话号码" />
          </el-form-item>
          <el-form-item prop="introduction" label="介绍" label-width="100px">
            <el-input
  type="textarea"
  :rows="2"
  placeholder="请输入内容"
  v-model="form.introduction">
</el-input>
          </el-form-item>

        </el-form>

  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary"  @click="submit">确 定</el-button>
  </div>
</el-dialog>
<!-- 编辑用户 -->
<el-dialog title="编辑用户" :visible.sync="dialogFormVisiblechange" >
       <el-form ref="form" :model="form" :rules="rules">
          <el-form-item prop="username" label="用户名" label-width="100px">
            <el-input v-model="form.username" placeholder="请输入用户名" />
          </el-form-item>
          <el-form-item prop="email" label="邮箱" label-width="100px">
            <el-input v-model="form.email" placeholder="请输入邮箱" />
          </el-form-item>
          <el-form-item prop="role" label="角色" label-width="100px">
            <el-input v-model="form.role" placeholder="请输入角色" />
          </el-form-item>
           <el-form-item label="权限组名称">
    <el-select v-model.number="form.permission_group_id" placeholder="活动区域">
      <el-option label="1" value="1"></el-option>
      <el-option label="asds" value="asds"></el-option>
      <el-option label="123" value="123"></el-option>
    </el-select>
           </el-form-item>
          <el-form-item prop="phone" label="联系电话" label-width="100px">
            <el-input v-model="form.phone" placeholder="请输入电话号码" />
          </el-form-item>
          <el-form-item prop="introduction" label="介绍" label-width="100px">
            <el-input
  type="textarea"
  :rows="2"
  placeholder="请输入内容"
  v-model="form.introduction">
</el-input>
          </el-form-item>
        </el-form>

  <div slot="footer" class="dialog-footer">
    <el-button @click="closeedit">取 消</el-button>
    <el-button type="primary"  @click="submitchange">确 定</el-button>
  </div>
</el-dialog>
<!-- 删除用户 -->
    </div>
  </div>
</template>

<script>
import { list, add, simple, update, remove } from '@/api/base/users.js'
export default {
  data () {
    return {
      // 弹出框
      dialogFormVisible: false,
      formLabelWidth: '100px',
      form: {
        id: '',
        username: '',
        password: '',
        email: '',
        role: '',
        permission_group_id: null,
        phone: '',
        introduction: '',
        // disabled: false,
        avatar: ''
      },
      rules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 12,
            message: '用户名格式不正确',
            trigger: 'blur'
          }
        ],
        email: [
          {
            required: true,
            message: '请输入邮箱',
            trigger: 'blur'
          },
          {
            // pattern: /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
            min: 3,
            max: 12,
            message: '邮箱格式错误',
            trigger: 'blur'
          }
        ],
        password: [
          {
            min: 3,
            max: 12,
            trigger: 'blur'
          }
        ]
      },
      // 编辑用户
      dialogFormVisiblechange: false,

      currentPage4: 1,
      input: '',
      tableData: [
      ],
      total: 0,
      info: {
        page: 1,
        pagesize: 10,
        keyword: '',
        disabled: null
      }
      // 添加用户
    }
  },

  async created () {
    this.getRenderList()
  },

  methods: {
    // 页数，条数
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.info.pagesize = val
      this.info.page = 1
      this.getRenderList()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.info.page = val
      this.getRenderList()
    },
    async getRenderList () {
      const { data } = await list(this.info)
      this.total = data.counts
      this.tableData = data.list
      this.info.page = 1
      this.info.pagesize = 10
    },
    // 清空
    formreset () {
      this.form.username = ' '
      this.form.password = ' '
      this.form.email = ' '
      this.form.role = ' '
      this.form.permission_group_id = ' '
      this.form.phone = ' '
      this.form.introduction = ' '
      this.form.avatar = ' '
    },
    empty () {
      this.input = ''
      this.getRenderList()
    },
    // 搜索
    select () {
      this.tableData = this.tableData.filter(item => item.username === this.input)
    },
    // 新增
    async submit () {
      try {
        this.dialogFormVisible = false
        await this.$refs.form.validate()
        await add(this.form)
        this.getRenderList()
        this.formreset()
      } catch (error) {
        console.log(error)
      }
    },
    // change
    async changeInfo (id) {
      this.dialogFormVisiblechange = true
      const data = await simple(id)
      const a = data.data.filter(item => item.id === id)
      this.form.id = id
      this.form.email = a[0].email
      this.form.phone = a[0].phone
      this.form.username = a[0].username
      this.form.permission_group_id = 1
    },
    async submitchange () {
      try {
        this.dialogFormVisiblechange = false
        // await this.$refs.form.validate()
        await update(this.form)

        this.formreset()

        this.getRenderList()
      } catch (error) {
        console.log(error)
      }
    },
    // 删除按钮
    open (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.form.id = id
        remove(this.form)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getRenderList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    closeedit () {
      this.dialogFormVisiblechange = false
      this.$refs.form.resetFields()
    }

  }
}
</script>

<style scoped lang="less">
.all {
  padding: 20px;
  .main {
    padding: 20px;
    background-color: #fff;
  }
  .separate {
    margin: 20px 0;
    width: 100%;
    height: 30px;
    background-color: #ccc;
    padding-left: 15px;
    border-radius: 5px;
    p {
      font-size: 18px;
      line-height: 30px;
    }
  }
}
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
.dialog-footer{
  text-align: center;
}
</style>
